﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ProjectTest.Models
{
    public class QLSinhVien
    {
        private DBTestDataContext db = new DBTestDataContext();
        

        //danh sach sinh vien
        public IEnumerable<SinhVien> GetAll()
        {
            return db.SinhViens;
        }
        public IEnumerable<SinhVien> FindByName(string name)
        {
            return db.SinhViens.Where(p => p.HoTenSV.ToLower().Contains(name.ToLower()));
        }
        //Create
        public void Create(SinhVien sv)
        {
            try
            {
                db.SinhViens.InsertOnSubmit(sv);
                db.SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
        //Edit
        public void Edit(SinhVien svEdited, string mssv)
        {
            SinhVien sv = db.SinhViens.SingleOrDefault(p => p.MaSV == mssv);
            try
            {
                UpdateInfo(sv, svEdited);
                db.SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void UpdateInfo(SinhVien sv, SinhVien svEdited)
        {
            sv.HoTenSV = svEdited.HoTenSV;
            sv.Lop = svEdited.Lop;
            sv.NgaySinh = svEdited.NgaySinh;
            sv.SDT = svEdited.SDT;
            sv.Email = svEdited.SDT;
        }
        //Delete
        public void Delete(string mssv)
        {
            SinhVien sv = db.SinhViens.SingleOrDefault(p => p.MaSV == mssv);
            try
            {
                db.SinhViens.DeleteOnSubmit(sv);
                db.SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }

    }
}